home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / database.mysql.inc < prev    next >
Encoding:
Text File  |  2005-04-14  |  6.1 KB  |  242 lines

  1. <?php
  2. // $Id: database.mysql.inc,v 1.27.2.1 2005/04/14 18:50:23 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Database interface code for MySQL database servers.
  7.  */
  8.  
  9. /**
  10.  * @ingroup database
  11.  * @{
  12.  */
  13.  
  14. /**
  15.  * Initialize a database connection.
  16.  *
  17.  * Note that you can change the mysql_connect() call to mysql_pconnect() if you
  18.  * want to use persistent connections. This is not recommended on shared hosts,
  19.  * and might require additional database/webserver tuning. It can increase
  20.  * performance, however, when the overhead to connect to your database is high
  21.  * (e.g. your database and web server live on different machines).
  22.  */
  23. function db_connect($url) {
  24.   $url = parse_url($url);
  25.  
  26.   // Allow for non-standard MySQL port.
  27.   if (isset($url['port'])) {
  28.      $url['host'] = $url['host'] .':'. $url['port'];
  29.   }
  30.  
  31.   $connection = mysql_connect($url['host'], $url['user'], $url['pass'], TRUE) or die(mysql_error());
  32.   mysql_select_db(substr($url['path'], 1)) or die('unable to select database');
  33.  
  34.   return $connection;
  35. }
  36.  
  37. /**
  38.  * Helper function for db_query().
  39.  */
  40. function _db_query($query, $debug = 0) {
  41.   global $active_db;
  42.   global $queries;
  43.  
  44.   if (variable_get('dev_query', 0)) {
  45.     list($usec, $sec) = explode(' ', microtime());
  46.     $timer = (float)$usec + (float)$sec;
  47.   }
  48.  
  49.   $result = mysql_query($query, $active_db);
  50.  
  51.   if (variable_get('dev_query', 0)) {
  52.     list($usec, $sec) = explode(' ', microtime());
  53.     $stop = (float)$usec + (float)$sec;
  54.     $diff = $stop - $timer;
  55.     $queries[] = array($query, $diff);
  56.   }
  57.  
  58.   if ($debug) {
  59.     print '<p>query: '. $query .'<br />error:'. mysql_error() .'</p>';
  60.   }
  61.  
  62.   if (!mysql_errno()) {
  63.     return $result;
  64.   }
  65.   else {
  66.     trigger_error(mysql_error() ."\nquery: ". htmlspecialchars($query), E_USER_ERROR);
  67.   }
  68. }
  69.  
  70. /**
  71.  * Fetch one result row from the previous query as an object.
  72.  *
  73.  * @param $result
  74.  *   A database query result resource, as returned from db_query().
  75.  * @return
  76.  *   An object representing the next row of the result. The attributes of this
  77.  *   object are the table fields selected by the query.
  78.  */
  79. function db_fetch_object($result) {
  80.   if ($result) {
  81.     return mysql_fetch_object($result);
  82.   }
  83. }
  84.  
  85. /**
  86.  * Fetch one result row from the previous query as an array.
  87.  *
  88.  * @param $result
  89.  *   A database query result resource, as returned from db_query().
  90.  * @return
  91.  *   An associative array representing the next row of the result. The keys of
  92.  *   this object are the names of the table fields selected by the query, and
  93.  *   the values are the field values for this result row.
  94.  */
  95. function db_fetch_array($result) {
  96.   if ($result) {
  97.     return mysql_fetch_array($result, MYSQL_ASSOC);
  98.   }
  99. }
  100.  
  101. /**
  102.  * Determine how many result rows were found by the preceding query.
  103.  *
  104.  * @param $result
  105.  *   A database query result resource, as returned from db_query().
  106.  * @return
  107.  *   The number of result rows.
  108.  */
  109. function db_num_rows($result) {
  110.   if ($result) {
  111.     return mysql_num_rows($result);
  112.   }
  113. }
  114.  
  115. /**
  116.  * Return an individual result field from the previous query.
  117.  *
  118.  * Only use this function if exactly one field is being selected; otherwise,
  119.  * use db_fetch_object() or db_fetch_array().
  120.  *
  121.  * @param $result
  122.  *   A database query result resource, as returned from db_query().
  123.  * @param $row
  124.  *   The index of the row whose result is needed.
  125.  * @return
  126.  *   The resulting field.
  127.  */
  128. function db_result($result, $row = 0) {
  129.   if ($result && mysql_num_rows($result) > $row) {
  130.     return mysql_result($result, $row);
  131.   }
  132. }
  133.  
  134. /**
  135.  * Determine whether the previous query caused an error.
  136.  */
  137. function db_error() {
  138.   return mysql_errno();
  139. }
  140.  
  141. /**
  142.  * Return a new unique ID in the given sequence.
  143.  *
  144.  * For compatibility reasons, Drupal does not use auto-numbered fields in its
  145.  * database tables. Instead, this function is used to return a new unique ID
  146.  * of the type requested. If necessary, a new sequence with the given name
  147.  * will be created.
  148.  */
  149. function db_next_id($name) {
  150.   $name = db_prefix_tables($name);
  151.   db_query('LOCK TABLES {sequences} WRITE');
  152.   $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
  153.   db_query("REPLACE INTO {sequences} VALUES ('%s', %d)", $name, $id);
  154.   db_query('UNLOCK TABLES');
  155.  
  156.   return $id;
  157. }
  158.  
  159. /**
  160.  * Determine the number of rows changed by the preceding query.
  161.  */
  162. function db_affected_rows() {
  163.   return mysql_affected_rows();
  164. }
  165.  
  166. /**
  167.  * Runs a limited-range query in the active database.
  168.  *
  169.  * Use this as a substitute for db_query() when a subset of the query is to be
  170.  * returned.
  171.  * User-supplied arguments to the query should be passed in as separate parameters
  172.  * so that they can be properly escaped to avoid SQL injection attacks.
  173.  *
  174.  * @param $query
  175.  *   A string containing an SQL query.
  176.  * @param ...
  177.  *   A variable number of arguments which are substituted into the query using
  178.  *   printf() syntax. Instead of a variable number of query arguments, you may
  179.  *   also pass a single array containing the query arguments.
  180.  * @param $from
  181.  *   The first result row to return.
  182.  * @param $count
  183.  *   The maximum number of result rows to return.
  184.  * @return
  185.  *   A database query result resource, or FALSE if the query was not executed
  186.  *   correctly.
  187.  */
  188. function db_query_range($query) {
  189.   $args = func_get_args();
  190.   $count = array_pop($args);
  191.   $from = array_pop($args);
  192.  
  193.   $query = db_prefix_tables($query);
  194.   if (count(func_get_args()) > 3) {
  195.     // Check for array (alternative syntax).
  196.     if (is_array($args[1])) {
  197.       $args = array_merge(array($query), $args[1]);
  198.     }
  199.     $args = array_map('db_escape_string', $args);
  200.     $args[0] = $query;
  201.     $query = call_user_func_array('sprintf', $args);
  202.   }
  203.   $query .= ' LIMIT '. $from .', '. $count;
  204.   return _db_query($query);
  205. }
  206.  
  207. /**
  208.  * Returns a properly formatted Binary Large OBject value.
  209.  *
  210.  * @param $data
  211.  *   Data to encode.
  212.  * @return
  213.  *  Encoded data.
  214.  */
  215. function db_encode_blob($data) {
  216.   return $data;
  217. }
  218.  
  219. /**
  220.  * Returns text from a Binary Large OBject value.
  221.  *
  222.  * @param $data
  223.  *   Data to decode.
  224.  * @return
  225.  *  Decoded data.
  226.  */
  227. function db_decode_blob($data) {
  228.   return $data;
  229. }
  230.  
  231. /**
  232.  * Prepare user input for use in a database query, preventing SQL injection attacks.
  233.  */
  234. function db_escape_string($text) {
  235.   return addslashes($text);
  236. }
  237.  
  238. /**
  239.  * @} End of "ingroup database".
  240.  */
  241.  
  242. ?>